﻿//  Created by Mathew Purchase.
//  Copyright (c) 2014 Mathew Purchase. All rights reserved.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class TurnService : MonoBehaviour 
{

	//---------------------------------------------------------------------------------------------
	// Properties
	public int totalTurns;
	public int turnCount;
	public int turnsPerDay;
	public int dayCount;
	
	
	
	
	//---------------------------------------------------------------------------------------------
	// Members
	
	public List<Light> LightCycle;
	
	
	//---------------------------------------------------------------------------------------------
	// Events and Delegates
	
	// set out turn phases
	// 1) turn start event
	// 2) run trade scripts
	// 3) run terrain scripts
	// 4) update ui
	// 5) allow player movement
	// 6) update player location
	// 7) turn end event
	
	public delegate void TurnManager();
	public static event TurnManager turnStart;
	public static event TurnManager turnEnd;
	public static event TurnManager DayEnd;
	public static event TurnManager gameStart;
	public static event TurnManager gameEnd;
	
	//---------------------------------------------------------------------------------------------
	// Init
	
	void Start () 
	{
		Character.playerMoved += endTurn;
	}
	
	void OnDisable ()
	{
		Character.playerMoved -= endTurn;
	}
	
	//---------------------------------------------------------------------------------------------
	// Core Functions
	
	public void startTurn()
	{
		if( turnStart != null)
		{
			turnStart();
		}
	}
	
	//---------------------------------------------------------------------------------------------
	public void endTurn()
	{
		incrementTurn();
		
		if( turnEnd != null)
		{
			turnEnd();
		}
		
		startTurn();
		
	}
	
	//---------------------------------------------------------------------------------------------
	public void incrementTurn()
	{
		totalTurns++;
		turnCount++;
		if ( turnCount > turnsPerDay)
		{
			turnCount = 0;
			dayCount ++;
			if (DayEnd != null)
			{
				Debug.Log (name + " Day Ended " + dayCount + " " + this );
				DayEnd();
			}
		}
	
	}
	
	
}
